// SPDX-License-Identifier: GPL-2.0
/*
 * sgm41620.c
 *
 * sgm41620 driver
 *
 * Copyright (c) 2021-2021 Huawei Technologies Co., Ltd.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 */
#include "sgmdev.h"
#include "sgmdev_pd.h"
#include "sgmdev_charger.h"
#include "tcpci.h"
#include "tcpci_core.h"
#include "tcpci_timer.h"
#include "tcpci_event.h"
#include "tcpm.h"
#include "pd_core.h"
#include "insdrv_gpio.h"

#undef LOG_TAG
#define LOG_TAG "sgm41620"
#include "elog.h"

volatile bool g_pd_isr_flag = false;
volatile bool g_chg_isr_flag = false;

struct sgm_device_info sgm_dev;

static void sgmdev_pd_irq_hander(void)
{
    g_pd_isr_flag = true;
}

static void __attribute__((unused)) sgmdev_charge_irq_handler(void)
{
    g_chg_isr_flag = true;
}

#if CHIP_TYPE == CHIP_SG41620

#define TCPC_INT_PORT INSDRV_GPIO_PORTC
#define TCPC_INT_PIN  INSDRV_GPIO_PIN_7

static void sgm41620_pd_irq_init(void)
{
    g_pd_isr_flag = false;

    InsDrv_Gpio_SetMode(TCPC_INT_PORT, TCPC_INT_PIN, INSDRV_GPIO_MODE_INPUT);
    InsDrv_Gpio_SetPullCtrl(TCPC_INT_PORT, TCPC_INT_PIN, INSDRV_GPIO_PULLUP);
    InsDrv_Gpio_IsrHook(TCPC_INT_PORT, TCPC_INT_PIN, sgmdev_pd_irq_hander);
    InsDrv_Gpio_EnableInt(TCPC_INT_PORT, TCPC_INT_PIN, INSDRV_GPIO_INT_FALLING);
}

static void sgm41620_charger_irq_init(void)
{
    /*
    g_chg_isr_flag = false;

    InsDrv_Gpio_SetMode(TCPC_INT_PORT, INSDRV_GPIO_PIN_8, INSDRV_GPIO_MODE_INPUT);
    InsDrv_Gpio_SetPullCtrl(TCPC_INT_PORT, INSDRV_GPIO_PIN_8, INSDRV_GPIO_PULLUP);
    InsDrv_Gpio_IsrHook(TCPC_INT_PORT, INSDRV_GPIO_PIN_8, sgmdev_charge_irq_handler);
    InsDrv_Gpio_EnableInt(TCPC_INT_PORT, INSDRV_GPIO_PIN_8, INSDRV_GPIO_INT_FALLING);
    */
}

static void sgm41620_tcpcdev_init(void)
{
    p_tcpc_dev->tcpc_vconn_supply = TCPC_VCONN_SUPPLY_ALWAYS;
#if RP_VALUE == 1
    p_tcpc_dev->typec_local_rp_level = TYPEC_RP_1_5;
#elif RP_VALUE == 2
    p_tcpc_dev->typec_local_rp_level = TYPEC_RP_3_0;
#else
    p_tcpc_dev->typec_local_rp_level = TYPEC_RP_DFT;
#endif
    p_tcpc_dev->typec_remote_rp_level = TYPEC_CC_VOLT_SNK_DFT;
    p_tcpc_dev->typec_polarity = false;

    p_tcpc_dev->tcpc_flags |= TCPC_FLAGS_ALERT_V10;
    p_tcpc_dev->tcpc_flags |= TCPC_FLAGS_PD_REV30;

    tcpc_device_wrap_ops();
    tcpc_device_register();
}

static void sgm41620_init_alert(void)
{
    sgm41620_pd_irq_init();
}

static int sgm41620_i2c_probe(void)
{
    int chip_id;

    chip_id = sgmdev_check_revision();
    if (chip_id < 0)
        return chip_id;
    sgm_dev.chip_id = (uint16_t)chip_id;
    log_i("sgm41620 chip id = 0x%x\n", chip_id);

    sgm41620_charger_irq_init();
    sgm41620_charger_hw_init();

    sgm_dev.pd_support = true;
    if (sgmdev_is_support_pd())
    {
        sgm41620_tcpcdev_init();
        sgm41620_init_alert();
#if SG41620_C_PATCH
        sgmdev_command(TCPM_CMD_ENABLE_VBUS_DETECT);
#endif
        tcpc_schedule_init_work();
    }

    if (sgmdev_is_support_ufcs())
    {
        // TBD
    }

    return 0;
}

#elif CHIP_TYPE == CHIP_SG795

#define TCPC_INT_PORT INSDRV_GPIO_PORTC
#define TCPC_INT_PIN  INSDRV_GPIO_PIN_7

static void sgm795_pd_irq_init(void)
{
    g_pd_isr_flag = false;

    InsDrv_Gpio_SetMode(TCPC_INT_PORT, TCPC_INT_PIN, INSDRV_GPIO_MODE_INPUT);
    InsDrv_Gpio_SetPullCtrl(TCPC_INT_PORT, TCPC_INT_PIN, INSDRV_GPIO_PULLUP);
    InsDrv_Gpio_IsrHook(TCPC_INT_PORT, TCPC_INT_PIN, sgmdev_pd_irq_hander);
    InsDrv_Gpio_EnableInt(TCPC_INT_PORT, TCPC_INT_PIN, INSDRV_GPIO_INT_FALLING);
}

//static void sgm795_charger_irq_init(void)
//{
//    g_chg_isr_flag = false;
//
//    InsDrv_Gpio_SetMode(TCPC_INT_PORT, INSDRV_GPIO_PIN_8, INSDRV_GPIO_MODE_INPUT);
//    InsDrv_Gpio_SetPullCtrl(TCPC_INT_PORT, INSDRV_GPIO_PIN_8, INSDRV_GPIO_PULLUP);
//    InsDrv_Gpio_IsrHook(TCPC_INT_PORT, INSDRV_GPIO_PIN_8, sgmdev_charge_irq_handler);
//    InsDrv_Gpio_EnableInt(TCPC_INT_PORT, INSDRV_GPIO_PIN_8, INSDRV_GPIO_INT_FALLING);
//}

static void sgm795_tcpcdev_init(void)
{
    p_tcpc_dev->tcpc_vconn_supply = TCPC_VCONN_SUPPLY_ALWAYS;

#if RP_VALUE == 1
    p_tcpc_dev->typec_local_rp_level = TYPEC_CC_RP_1_5;
#elif RP_VALUE == 2
    p_tcpc_dev->typec_local_rp_level = TYPEC_CC_RP_3_0;
#else
    p_tcpc_dev->typec_local_rp_level = TYPEC_CC_RP_DFT;
#endif

    p_tcpc_dev->tcpc_flags |= TCPC_FLAGS_ALERT_V10;
    p_tcpc_dev->tcpc_flags |= TCPC_FLAGS_PD_REV30;

    tcpc_device_wrap_ops();
    tcpc_device_register();
}

static void sgm795_init_alert(void)
{
    sgm795_pd_irq_init();
}

static int sgm795_i2c_probe(void)
{
    int chip_id;

    chip_id = sgmdev_check_revision();
    if (chip_id < 0)
        return chip_id;
    sgm_dev.chip_id = (uint16_t)chip_id;
    log_i("sgm41620 chip id = 0x%x\n", chip_id);

//    sgm795_charger_irq_init();
//    sgm795_charger_hw_init();

    sgm_dev.pd_support = true;
    if (sgmdev_is_support_pd())
    {
        sgm795_tcpcdev_init();
        sgm795_init_alert();
//        sgmdev_command(TCPM_CMD_ENABLE_VBUS_DETECT);
        tcpc_schedule_init_work();
    }

    if (sgmdev_is_support_ufcs())
    {
        // TBD
    }

    return 0;
}

#endif // #if CHIP_TYPE == CHIP_SG41620

int sgmdev_i2c_probe(void)
{
#if CHIP_TYPE == CHIP_SG41620
    return sgm41620_i2c_probe();
#elif CHIP_TYPE == CHIP_SG795
    return sgm795_i2c_probe();
#endif
}

